import SwiftUI
//import MapboxMaps
import CoreLocation

struct MapView: View {
    @StateObject private var locationManager = LocationManager()
    @Environment(\.presentationMode) var presentationMode

    var body: some View {
        NavigationView {
            VStack (spacing:0) {
                Back(title: "Map"){
                    self.presentationMode.wrappedValue.dismiss()
                }
//                ZStack {
//                    VStack {
//                        if let center = locationManager.location {
//                            Map(initialViewport: .camera(center: center, zoom: 16, bearing: 0, pitch: 0)){
//                                Puck2D(bearing: .heading)
//                            }
//                            .ignoresSafeArea()
//                        } else {
////                            
//                            VStack{
//                                AnimationMulticolorView(letters:"Loading...").padding(.top,50)
//                            }
//                               
//                        }
//                    }
//                } .onAppear {
//                    locationManager.requestLocation()
//                }
                Spacer()
            }.navigationBarHidden(true)
                .navigationViewStyle(StackNavigationViewStyle())
            // .navigationViewStyle(StackNavigationViewStyle())
            
        }.navigationBarHidden(true)
            .navigationViewStyle(StackNavigationViewStyle())
        // .navigationViewStyle(StackNavigationViewStyle())
        
    }
}

final class LocationManager: NSObject, ObservableObject, CLLocationManagerDelegate {
    private let locationManager = CLLocationManager()
    @Published var location: CLLocationCoordinate2D?
    
    override init() {
        super.init()
        locationManager.delegate = self
        locationManager.requestWhenInUseAuthorization()
    }
    
    func requestLocation() {
        locationManager.requestLocation()
    }
    
    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        guard let newLocation = locations.last else { return }
        self.location = newLocation.coordinate
    }
    
    func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
        print("Failed to get location: \(error.localizedDescription)")
    }
}

#Preview {
    MapView()
}



struct WaveTextAnimation: View {
    @State private var isAnimating = false
    @State private var offsets: [CGFloat] = []
    let text = "Loading..."
    let waveHeight: CGFloat = 10 // 设置为较小值
    let speed: Double = 0.05 // 控制动画的速度

    var body: some View {
        VStack {
            HStack(spacing: 0) {
                ForEach(Array(text.enumerated()), id: \.offset) { index, character in
                    Text(String(character))
                        .font(.system(size: 40))
                        .offset(y: offsets.indices.contains(index) ? offsets[index] : 0) // 检查索引范围
                }
            }
            .onAppear {
                // 初始化 offsets 数组
                offsets = Array(repeating: 0, count: text.count)
                startAnimation()
            }
        }
        .padding(.top, 50)
    }

    private func startAnimation() {
        var phase: CGFloat = 0

        Timer.scheduledTimer(withTimeInterval: speed, repeats: true) { timer in
            for index in offsets.indices {
                // 计算新的偏移量
                offsets[index] = waveHeight * sin(CGFloat(index) + phase)
            }
            phase += 0.2 // 控制波动的频率
        }
    }
}

struct WaveTextAnimation_Previews: PreviewProvider {
    static var previews: some View {
        WaveTextAnimation()
    }
}
